package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.LifeRecordRequest;
import com.ruoyi.livedemo.dto.PagedResult;
import com.ruoyi.livedemo.entity.LifeRecord;
import com.ruoyi.livedemo.service.LifeRecordService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/life-records")
public class LifeRecordController {

    private final LifeRecordService lifeRecordService;

    @GetMapping
    public ApiResponse<PagedResult<LifeRecord>> list(@RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "10") int size) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(lifeRecordService.list(userId, page, size));
    }

    @PostMapping
    public ApiResponse<LifeRecord> create(@Valid @RequestBody LifeRecordRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("生活瞬间已记录", lifeRecordService.create(userId, request));
    }

    @PutMapping("/{id}")
    public ApiResponse<LifeRecord> update(@PathVariable Long id, @Valid @RequestBody LifeRecordRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("生活瞬间已更新", lifeRecordService.update(userId, id, request));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        lifeRecordService.delete(userId, id);
        return ApiResponse.ok("生活瞬间已删除", null);
    }
}